package com.example.jpetstoremanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.jpetstoremanager.persistence.UserMapper;

import com.example.jpetstoremanager.entity.User;
import com.example.jpetstoremanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
//    @Autowired
//    private AccountMapper accountMapper;

    public User findUserById(int id){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return userMapper.selectOne(queryWrapper);
    }

    public User findUserByUsername(String username){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.selectOne(queryWrapper);
    }
    public List<User> findAllUsers(){
        return userMapper.selectList(null);
    }

    public List<User> findAdminUser(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("isAdmin",1);
        return userMapper.selectList(queryWrapper);
    }

    //查询非管理员
    public List<User> findNotAdminUser(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("isAdmin",0);
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public boolean IsUserExist(int id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User user = userMapper.selectOne(queryWrapper);
        return user!=null;
    }

    //改
    // 包括编辑用户名、密码
    //先初始化好一个user传进来update
    public void updateUserById(User user){
        userMapper.updateById(user);
    }
    //增（同时增加Account中的信息）
    public void insertUser(User user){
        userMapper.insert(user);
    }
    public boolean IsUserExistByUsername(String username){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user=userMapper.selectOne(queryWrapper);
        return user != null;
    }
    //删（同时删除Account中的信息）
    public void deleteUserById(int userid){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",userid);
        userMapper.delete(queryWrapper);
//        QueryWrapper<Account> queryWrapper1=new QueryWrapper<>();
//        queryWrapper1.eq("userid",userid);
//        accountMapper.delete(queryWrapper1);

    }
    public void deleteUserByUsername(String username){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        userMapper.delete(queryWrapper);
//        QueryWrapper<Account> queryWrapper1=new QueryWrapper<>();
//        queryWrapper1.eq("firstname",username);
//        accountMapper.delete(queryWrapper1);
    }
}
